package com.visionet.waveletData.web.repository.plan.impl;

import com.visionet.waveletData.web.entity.plan.PlayGroup;
import com.visionet.waveletData.web.repository.plan.PlanGroupDao;
import com.visionet.waveletData.web.vo.plan.PlanGroupVo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;


@Repository
public class PlanGroupDaoImpl{


    private final static Logger logger = LoggerFactory.getLogger(PlanGroupDao.class);

    @Resource
    private JdbcTemplate jdbcTemplate;

    public List<PlanGroupVo> findPlanGroupVoByPlanId(String planId){
        StringBuffer sql = new StringBuffer();
        sql.append("select p.id as id,g.group_name as groupName,p.group_id as groupId, g.robot_number as robotNumber from p_plan_group p left join p_group g  on p.group_id = g.id where p.plan_id='"+planId+"' and p.is_deleted='0' ");
        List<PlanGroupVo> planGroupVos = jdbcTemplate.query(sql.toString(), new RowMapper<PlanGroupVo>() {
			@Override
			public PlanGroupVo mapRow(ResultSet rs, int rowNum)
					throws SQLException {
				PlanGroupVo vo = new PlanGroupVo();
				vo.setId(rs.getLong("id"));
				vo.setGroupId(rs.getString("groupId"));
				vo.setGroupName(rs.getString("groupName"));
				vo.setRobotNumber(rs.getInt("robotNumber"));
				return vo;
			}
        });
        return planGroupVos;
       
    }
    public List<PlayGroup> findPlanGroupByPlanIdAndGroupId(String planId,String groupId){
    	StringBuffer sql = new StringBuffer();
        sql.append("select p.id from p_plan_group p  where p.plan_id='"+planId+"' and p.group_id='"+groupId+"' and p.is_deleted='0' ");
        List<PlayGroup> planGroups = jdbcTemplate.query(sql.toString(), new RowMapper<PlayGroup>() {
			@Override
			public PlayGroup mapRow(ResultSet rs, int rowNum)
					throws SQLException {
				PlayGroup vo = new PlayGroup();
				vo.setId(rs.getLong("id"));
				return vo;
			}
        });
        return planGroups;
    }


	public void deletePlanGroup(List<String> ids){
		if(ids != null){
			StringBuffer sql = new StringBuffer();
			sql.append("update p_plan_group as ppg set ppg.is_deleted = '1' where ppg.id in ( ");
			for(String s : ids){
				sql.append(s).append(",");
			}
			String sqlStr = sql.substring(0,sql.length()-1);
			sqlStr = sqlStr + ")";
			jdbcTemplate.execute(sqlStr);
		}

	}
}
